<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item>
              <el-input v-model="query.name" placeholder="类型名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <userSelect v-model="query.creatorUserId" placeholder="创建人" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-date-picker v-model="query.creatorTime" value-format="timestamp" format="yyyy-MM-dd"
                start-placeholder="创建开始日期" end-placeholder="创建结束日期" type="daterange" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <el-button v-has="'btn_add'" type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
          </div>
          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
          </div>
        </div>
        <JNPF-table v-loading="listLoading" :data="list" @sort-change='sortChange' :customColumn="true">
          <el-table-column prop="name" label="类型名称" align="left" />
          <el-table-column align="center" label="是否启用" prop="enabledMark">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.enabledMark" :active-value="1" :inactive-value="0" @change="enabledMarkChange($event, scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="creatorTime" label="创建时间" align="left" min-width="140" />
          <el-table-column prop="creatorUserName" label="创建人" align="left" />
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button v-has="'btn_edit'" type="text" :disabled="scope.row.defaultMark == 1" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
              <el-button v-has="'btn_remove'" type="text" class="JNPF-table-delBtn" :disabled="scope.row.defaultMark == 1" @click="handleDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
      </div>
    </div>
    <JNPF-Form v-if="formVisible" ref="JNPFForm" @refreshAssoInfoType="refreshAssoInfoType" />
  </div>
</template>

<script>
  import { getAssoInfoTypeList, delAssoInfoType, updateAssoInfoType } from '@/api/asso/info/category'
  import JNPFForm from './Form'

  export default {
    components: {
      JNPFForm
    },
    data() {
      return {
        query: {
          name: undefined,
          creatorUserId: undefined,
          creatorTime: undefined,
        },
        treeProps: {
          children: 'children',
          label: 'fullName',
          value: 'id'
        },
        list: [],
        listLoading: true,
        total: 0,
        listQuery: {
          superQueryJson: '',
          currentPage: 1,
          pageSize: 20,
          sort: "desc",
          sidx: "",
        },
        formVisible: false,
      }
    },
    created() {
        this.initData()
    },
    methods: {
      sortChange({
        column,
        prop,
        order
      }) {
        this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
        this.listQuery.sidx = !order ? '' : prop
        this.initData()
      },
      initData() {
        this.listLoading = true;
        let _query = {
          ...this.listQuery,
          ...this.query,
          keyword: this.keyword,
          dataType: 0
        };
        getAssoInfoTypeList(_query).then((res) => {
          this.list = JSON.parse(JSON.stringify(res.data.list))
          this.total = res.data.pagination.total
          this.listLoading = false
        }).catch(()=>{
          this.listLoading = false
        })
      },
      handleDel(id) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          type: 'warning'
        }).then(() => {
          delAssoInfoType(id).then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        }).catch(() => {});
      },
      addOrUpdateHandle(id) {
        this.formVisible = true
        this.$nextTick(() => {
          this.$refs.JNPFForm.init(id)
        })
      },
      search() {
        this.listQuery.currentPage = 1
        this.listQuery.pageSize = 20
        this.listQuery.sort = "desc"
        this.listQuery.sidx = ""
        this.initData()
      },
      refreshAssoInfoType(isrRefresh) {
        this.formVisible = false
        if (isrRefresh) this.reset()
      },
      reset() {
        for (let key in this.query) {
          this.query[key] = undefined
        }
        this.search()
      },
      enabledMarkChange(e, row) {
        updateAssoInfoType(row.id, row).then((res) => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.initData()
            }
          })
        })
      }
    }
  }
</script>
